package com.cvte.shop.service.rest;

import com.cvte.csb.core.interfaces.vo.RestResponse;
import com.cvte.csb.web.rest.ResponseFactory;
import com.cvte.csb.web.swagger.annonation.ApiResponse;
import com.cvte.csb.web.swagger.annonation.ApiResponseDoc;
import com.cvte.csb.web.swagger.annonation.ApiVersion;
import com.cvte.csb.web.swagger.annonation.ApiVersionDoc;
import com.cvte.shop.annotation.EableLog;
import com.cvte.shop.constants.UserConstants;
import com.cvte.shop.enums.RestResponceEnum;
import com.cvte.shop.service.ShopUserAllService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author DaiShubo
 * @version 1.0
 * @description 登陆相关的接口
 * @createTime 2020/7/28 5:21 下午
 * @since 1.7
 **/
@Api(tags = "登陆等相关接口")
@RestController
@RequestMapping("/login")
public class RestLoginController {

    @Autowired
    private ShopUserAllService shopUserAllService;

    /**
     * 登陆
     *
     * @param username   用户名
     * @param password   密码
     * @param request
     * @return
     */
    @ApiResponseDoc({
            @ApiResponse(key = "username", value = String.class),
            @ApiResponse(key = "password", value = String.class)
    })
    @ApiVersionDoc(
            {
                    @ApiVersion(value = 1, desc = "初版")
            }
    )
    @ApiOperation("登陆")
    @PostMapping("/login")
    public RestResponse login(String username, String password, HttpServletRequest request) {
        return shopUserAllService.verifyUser(username, password, request);
    }

    /**
     * 注册功能
     * @param username      用户名
     * @param password      密码
     * @return
     */
    @EableLog
    @ApiResponseDoc({
            @ApiResponse(key = "username", value = String.class),
            @ApiResponse(key = "password", value = String.class)
    })
    @ApiVersionDoc(
            {
                    @ApiVersion(value = 1, desc = "初版")
            }
    )
    @ApiOperation("注册")
    @PostMapping("/register")
    public RestResponse register(String username,String password){
        shopUserAllService.register(username,password);
        return ResponseFactory.buildResult("注册成功！");
    }


    /**
     * 未登录
     * @param response
     * @return
     */
    @ApiVersionDoc(
            {
                    @ApiVersion(value = 1, desc = "初版")
            }
    )
    @ApiOperation("未登陆")
    @GetMapping("/un_login")
    public RestResponse un_login(HttpServletResponse response){
        return ResponseFactory.getUnauthorizedResult(RestResponceEnum.UN_LOGIN.getMessage(),RestResponceEnum.UN_LOGIN.getCode(),response);
    }


}
